package com.ymatou.iapi.couponmanage.old.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.old.parameter.GenerateCouponBean;
import com.ymatou.iapi.couponmanage.old.service.GenerateCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponGenerateSubTask;
import com.ymttest.database.model.YmtCouponGenerateTask;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateSubTaskWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateTaskWapper;
/**
*生成优惠卷接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GenerateCoupon{
	private static GenerateCouponBean generatecouponBean;
	private static GenerateCouponCall generatecouponCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("生成优惠卷接口");
	}

	@Before
	public void caseUp() {
		generatecouponBean=new GenerateCouponBean();
		generatecouponCall=new GenerateCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(generatecouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GenerateCouponList_001() {
		Logger.start(false,"生成优惠卷-subTask不存在");
		try {

			generatecouponCall.setData(generatecouponBean);
			generatecouponCall.callService();			
			
			Logger.verifyEquals("\"获取子任务失败\"", generatecouponCall.getReturnData(),
					"验证返回信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GenerateCouponList_002() {
		Logger.start(true,"生成优惠卷-subTask:4生成完成");
		try {
			YmtCouponGenerateSubTaskWapper ymtCouponGenerateSubTaskWapper=new YmtCouponGenerateSubTaskWapper();
			YmtCouponGenerateSubTask subTask=ymtCouponGenerateSubTaskWapper.selectTop1().get(0);
			
			ymtCouponGenerateSubTaskWapper.setSubTaskStatus(subTask.getSubTaskId(), 4);
			
			generatecouponBean.setTaskItemId(subTask.getSubTaskId());
			
			generatecouponCall.setData(generatecouponBean);
			generatecouponCall.callService();			
			
			Logger.verifyEquals("\"ok\"", generatecouponCall.getReturnData(),
					"验证返回信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GenerateCouponList_004() {
		Logger.start(true,"生成优惠卷-subTask:1待加入JOB");
		try {
			YmtCouponGenerateSubTaskWapper ymtCouponGenerateSubTaskWapper=new YmtCouponGenerateSubTaskWapper();
			YmtCouponGenerateSubTask subTask=ymtCouponGenerateSubTaskWapper.selectTop1().get(0);

			int SubTaskStatus = subTask.getSubTaskStatus();
			
			ymtCouponGenerateSubTaskWapper.setSubTaskStatus(subTask.getSubTaskId(), 1);
			
			generatecouponBean.setTaskItemId(subTask.getSubTaskId());
			
			generatecouponCall.setData(generatecouponBean);
			generatecouponCall.callService();			
			
			Logger.verifyEquals("\"子任务还未加入定时器暂不生成\"", generatecouponCall.getReturnData(),
					"验证返回信息");
			
			ymtCouponGenerateSubTaskWapper.setSubTaskStatus(subTask.getSubTaskId(), SubTaskStatus);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GenerateCouponList_005() {
		Logger.start(true,"生成优惠卷-subTask:3加入失败");
		try {
			YmtCouponGenerateSubTaskWapper ymtCouponGenerateSubTaskWapper=new YmtCouponGenerateSubTaskWapper();
			YmtCouponGenerateSubTask subTask=ymtCouponGenerateSubTaskWapper.selectTop1().get(0);
			
			int SubTaskStatus = subTask.getSubTaskStatus();
			
			ymtCouponGenerateSubTaskWapper.setSubTaskStatus(subTask.getSubTaskId(), 3);
			
			generatecouponBean.setTaskItemId(subTask.getSubTaskId());
			
			generatecouponCall.setData(generatecouponBean);
			generatecouponCall.callService();			
			
			Logger.verifyEquals("\"子任务还未加入定时器暂不生成\"", generatecouponCall.getReturnData(),
					"验证返回信息");
			
			ymtCouponGenerateSubTaskWapper.setSubTaskStatus(subTask.getSubTaskId(), SubTaskStatus);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GenerateCouponList_003() {
		Logger.start(true,"生成优惠卷-获取任务失败");
		try {
			YmtCouponGenerateSubTaskWapper ymtCouponGenerateSubTaskWapper=new YmtCouponGenerateSubTaskWapper();
			YmtCouponGenerateSubTask subTask=ymtCouponGenerateSubTaskWapper.selectTop1BySubTaskStatus(2).get(0);
			
			int taskid=subTask.getTaskId();
			
			ymtCouponGenerateSubTaskWapper.setTaskId(subTask.getSubTaskId(), 99999999);
			
			generatecouponBean.setTaskItemId(subTask.getSubTaskId());
			
			generatecouponCall.setData(generatecouponBean);
			generatecouponCall.callService();			
			
			Logger.verifyEquals("\"获取任务失败\"", generatecouponCall.getReturnData(),
					"验证返回信息");
			
			//ymtCouponGenerateSubTaskWapper.setTaskId(subTask.getSubTaskId(), taskid);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GenerateCouponList_006() {
		Logger.start(true,"生成优惠卷-获取批次失败");
		try {
			YmtCouponGenerateSubTaskWapper ymtCouponGenerateSubTaskWapper=new YmtCouponGenerateSubTaskWapper();
			YmtCouponGenerateTaskWapper ymtCouponGenerateTaskWapper=new YmtCouponGenerateTaskWapper();
			
			YmtCouponGenerateSubTask subTask=ymtCouponGenerateSubTaskWapper.selectTop1().get(0);
			int taskid=subTask.getTaskId();			
			ymtCouponGenerateSubTaskWapper.setSubTaskStatus(subTask.getSubTaskId(), 2);
			
			YmtCouponGenerateTask task=ymtCouponGenerateTaskWapper.selectByPrimaryKey(taskid);
			int batchid=task.getCouponBatchId();
			
			ymtCouponGenerateTaskWapper.setCouponBatchId(taskid, 99999999);
			
			generatecouponBean.setTaskItemId(subTask.getSubTaskId());
			
			generatecouponCall.setData(generatecouponBean);
			generatecouponCall.callService();			
			
			Logger.verifyEquals("\"获取批次失败\"", generatecouponCall.getReturnData(),
					"验证返回信息");
			
			ymtCouponGenerateTaskWapper.setCouponBatchId(taskid, batchid);
			
		} catch (Exception e) {             
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GenerateCouponList_007() {
		Logger.start(true,"生成优惠卷-获取优惠卷设置失败");
		try {
			YmtCouponGenerateSubTaskWapper ymtCouponGenerateSubTaskWapper=new YmtCouponGenerateSubTaskWapper();
			YmtCouponGenerateTaskWapper ymtCouponGenerateTaskWapper=new YmtCouponGenerateTaskWapper();
			YmtCouponBatchWapper ymtCouponBatchWapper = new YmtCouponBatchWapper();
			
			YmtCouponGenerateSubTask subTask=ymtCouponGenerateSubTaskWapper.selectTop1().get(0);
			int taskid=subTask.getTaskId();		
			
			ymtCouponGenerateSubTaskWapper.setSubTaskStatus(subTask.getSubTaskId(), 2);
			
			YmtCouponGenerateTask task=ymtCouponGenerateTaskWapper.selectByPrimaryKey(taskid);
			int batchid=task.getCouponBatchId();
			
			YmtCouponBatch batch=ymtCouponBatchWapper.selectByPrimaryKey(batchid);
			int settingId=batch.getiCouponSettingId();
			
			ymtCouponBatchWapper.setiCouponSettingId(batchid, 99999999);
			
			generatecouponBean.setTaskItemId(subTask.getSubTaskId());
			
			generatecouponCall.setData(generatecouponBean);
			generatecouponCall.callService();			
			
			Logger.verifyEquals("\"获取优惠卷设置失败\"", generatecouponCall.getReturnData(),
					"验证返回信息");			

			ymtCouponGenerateTaskWapper.setCouponBatchId(taskid, settingId);
			
		} catch (Exception e) {             
			Logger.fail(e);
		}
	}
}